package com.hezhiqin.zk;

import org.apache.zookeeper.*;

import java.io.IOException;

/**
 * @version v1.0
 * @ProjectName: homeWork
 * @ClassName: WatcherDemo2
 * @Description: TODO(一句话描述该类的功能)
 * @Author: Administrator
 * @Date: 2022/2/10 17:28
 */

public class WatcherDemo2 {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zookeeper = new ZooKeeper("192.168.187.10:2181,192.168.187.11:2181,192.168.187.12:2181", 4000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("event.type" + event.getType());
            }
        });
        // 创建节点 zookeeper.exists("/watch",true);
        // 注册监听 Thread.sleep(1000); zookeeper.setData("/watch", "1".getBytes(),-1) ;
        // 修改节点的值触发监听
        zookeeper.create("/watch", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.in.read();
    }
}